//
//  CLWebVeiwController.m
//  chanlin
//
//  Created by Ryan on 15-1-5.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLWebVeiwController.h"
#import "IFAudioPlayerManager.h"

@interface CLWebVeiwController ()

@property UIWebView *webView;

@property (nonatomic,strong)IFDocAction *action;
@end


@implementation IFDocAction

-(void)pop{
  
     [[self k_currentController].dispatchObj pop];
}

@end

@implementation CLWebVeiwController

- (void)dealloc{
    NSLog(@"dealloc web");
}


- (void)viewDidLoad {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    [super viewDidLoad];
    if([IFAudioPlayerManager shareInstance].isPlaying){
        [[IFAudioPlayerManager shareInstance] stop];
    }
    self.view.backgroundColor=[UIColor whiteColor];
    self.webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0.f, GlobleWidth, GlobleHeight)];
    self.webView.backgroundColor=[UIColor whiteColor];
    self.webView.allowsInlineMediaPlayback=YES;
    
    
    
    
    
    self.webView.scalesPageToFit=YES;
    [self.view addSubview:self.webView];
    NSURL *url=self.isFile?[NSURL fileURLWithPath:self.itemId]:[NSURL URLWithString:self.itemId];
    
    NSURLRequest *request=[NSURLRequest requestWithURL:url];

  
    [self.webView loadRequest:request];
    
    self.jsContext=[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.action=[IFDocAction new];
    self.jsContext[@"appNativeApi"]=self.action;

    [self showToolbarWithType:IFActive_ButtomToolBarButtonTypeShare];
    
    __weak typeof(self) me=self;
    [self.tool handleTap:IFActive_ButtomToolBarButtonTypeShare block:^(id sender) {
        
        
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic sgrSetObject:[UIImage imageNamed:@"icon114.png"] forKey:@"icon"];
        
       NSString *currentTitle = [me.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        
        [dic sgrSetObject:_safeStr(currentTitle) forKey:@"title"];
        
        
        [dic sgrSetObject:me.itemId  forKey:@"url"];
        [me openShare:dic];
    }];
    
    CGRect frame1=[self.tool.view convertRect:self.tool.backButton.frame toView:self.view];
    CGRect frame2=[self.tool.view convertRect:self.tool.shareButton.frame toView:self.view];
    [self.tool.view removeFromSuperview];
    [self.view addSubview:self.tool.backButton];
    [self.view addSubview:self.tool.shareButton];
    self.tool.backButton.frame=frame1;
    self.tool.shareButton.frame=frame2;
    
  
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)showPopAction{
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    self.jsContext=[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.action=[IFDocAction new];
    self.jsContext[@"appNativeApi"]=self.action;
}



@end
